package com.lecture.java8.time;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class TimeParam {
    private Date startTime;
    private Date endTime;

    public TimeParam(Date startTime, Date endTime) {
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    @Override
    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return "TimeParam{" +
                "startTime=" + sdf.format(startTime) +
                ", endTime=" + sdf.format(endTime) +
                '}';
    }

    public static void main(String[] args) {
        List<TimeParam> timeParams = generateMonthlyTimeParams();
        for (TimeParam tp : timeParams) {
            System.out.println(tp);
        }
    }

    public static List<TimeParam> generateMonthlyTimeParams() {
        List<TimeParam> timeParams = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Calendar current = Calendar.getInstance();
        Date now = new Date();

        // Set the start time to the beginning of the year
        calendar.set(Calendar.YEAR, current.get(Calendar.YEAR));
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        while (calendar.getTime().before(now)) {
            Date startTime = calendar.getTime();
            calendar.add(Calendar.MONTH, 1);
            Date endTime = calendar.getTime();

            // Adjust the endTime for the last month to be the current time
            if (endTime.after(now)) {
                endTime = now;
            }

            timeParams.add(new TimeParam(startTime, endTime));
        }

        return timeParams;
    }


}

